﻿using Application = System.Windows.Application;
using FileSecurityManager.BizLogic;

namespace FileSecurityManager.UI.UserControls
{
    using System.Windows.Controls;

    using FileSecurityManager.BizEntities;
    using FileSecurityManager.BizLogic.Exceptions;

    /// <summary>
    /// Interaction logic for UsersCtl.xaml
    /// </summary>
    public partial class UsersCtl
    {
        private readonly UsersLogic logic = new UsersLogic();

        public UsersCtl()
        {
            InitializeComponent();
        }

        private void loadUsers()
        {
            // binding data grid
            usersGrid.ItemsSource = logic.GetAllUsers();
            usersGrid.SelectedIndex = -1;
        }

        private void btnAddNewUser_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var dialog = new UserInfo(null);
            dialog.ShowDialog();
            if (dialog.DialogResult == true)
            {
                loadUsers();
            }
        }

        /// <summary>
        /// Handles the Loaded event of the UserControl control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void userControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            loadUsers();
        }

        /// <summary>
        /// Handles the Click event of the btnExit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnExit_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Application.Current.Shutdown(0);
        }

        private void tbEdit_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                TextBlock tb = sender as TextBlock;
                if (tb != null && tb.Tag is UserModel)
                {
                    var user = tb.Tag as UserModel;

                    var dialog = new UserInfo(user);
                    dialog.ShowDialog();
                    if (dialog.DialogResult == true)
                    {
                        loadUsers();
                    }
                }
            }
            catch (BizLogicException)
            {
            }
        }

        private void tbDelete_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                TextBlock tb = sender as TextBlock;
                if (tb != null && tb.Tag is UserModel)
                {
                    var user = tb.Tag as UserModel;
                    logic.DeleteUser(user.ItemId);
                    loadUsers();
                }
            }
            catch (BizLogicException)
            {
            }
        }
    }
}
